# This is a sample Python script.

# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.
import json
import os
import os.path


def print_hi(name):
    # Use a breakpoint in the code line below to debug your script.
    print(f'Hi, {name}')  # Press Ctrl+F8 to toggle the breakpoint.


def get_flop_feature(flop_board):
    card_num_dic = {
        '2': 2,
        '3': 3,
        '4': 4,
        '5': 5,
        '6': 6,
        '7': 7,
        '8': 8,
        '9': 9,
        'T': 10,
        'J': 11,
        'Q': 12,
        'K': 13,
        'A': 14,
    }
    flop_feature = {}
    flop_cards = [
        flop_board[0:2],
        flop_board[2:4],
        flop_board[4:]
    ]
    if flop_cards[0][1] == flop_cards[1][1] == flop_cards[2][1]:
        flop_feature['suits_num'] = 1
    elif flop_cards[0][1] != flop_cards[1][1] and flop_cards[0][1] != flop_cards[2][1] \
            and flop_cards[1][1] != flop_cards[2][1]:
        flop_feature['suits_num'] = 3
    else:
        flop_feature['suits_num'] = 2

    if flop_cards[0][0] == flop_cards[1][0] == flop_cards[2][0]:
        flop_feature['card_num_differ'] = 1
    elif flop_cards[0][0] != flop_cards[1][0] and flop_cards[0][1] != flop_cards[2][0] \
            and flop_cards[1][0] != flop_cards[2][0]:
        flop_feature['card_num_differ'] = 3
    else:
        flop_feature['card_num_differ'] = 2

    flop_feature['card_num_max'] = card_num_dic[flop_cards[0][0]]
    flop_feature['card_num_middle'] = card_num_dic[flop_cards[1][0]]
    flop_feature['card_num_min'] = card_num_dic[flop_cards[2][0]]
    flop_feature['card_total_num'] = card_num_dic[flop_cards[0][0]] + card_num_dic[flop_cards[1][0]] + card_num_dic[
        flop_cards[2][0]]

    if card_num_dic[flop_cards[0][0]] != 14:
        flop_feature['card_connect_num'] = card_num_dic[flop_cards[0][0]] - card_num_dic[flop_cards[2][0]]
        card_connect_0_1 = card_num_dic[flop_cards[0][0]] - card_num_dic[flop_cards[1][0]]
        card_connect_1_2 = card_num_dic[flop_cards[1][0]] - card_num_dic[flop_cards[2][0]]
        flop_feature['card_connect_max'] = max(card_connect_0_1, card_connect_1_2)
        flop_feature['card_connect_min'] = min(card_connect_0_1, card_connect_1_2)
    else:
        flop_feature['card_connect_num'] = min(14 - card_num_dic[flop_cards[2][0]], card_num_dic[flop_cards[1][0]] - 1)
        if card_num_dic[flop_cards[1][0]] != 14:
            card_connect_0_1 = 14 - card_num_dic[flop_cards[1][0]]
            card_connect_1_0 = card_num_dic[flop_cards[1][0]] - 1
            card_connect_1_2 = card_num_dic[flop_cards[1][0]] - card_num_dic[flop_cards[2][0]]
            card_connect_2_0 = card_num_dic[flop_cards[2][0]] - 1
            card_connect_0_2 = 14 - card_num_dic[flop_cards[2][0]]

            if (card_connect_0_1 >= card_connect_1_0):
                flop_feature['card_connect_max'] = max(card_connect_1_2, card_connect_2_0)
            else:
                flop_feature['card_connect_max'] = max(card_connect_0_1, card_connect_1_2)

            flop_feature['card_connect_min'] = min(card_connect_0_1, card_connect_1_0, card_connect_1_2,
                                                   card_connect_2_0, card_connect_0_2)
        else:
            flop_feature['card_connect_max'] = min(14 - card_num_dic[flop_cards[2][0]],
                                                   card_num_dic[flop_cards[2][0]] - 1)
            flop_feature['card_connect_min'] = 0

    return flop_feature


def analyze_poker_file(file_full_path, folder_name):
    with open(file_full_path, 'r') as f:
        lines = f.readlines()
        flop_strategies = []
        array_len = 0
        table_column = [
            {
                'prop': 'flop_board',
                'title': '翻牌',
            },
            {
                'prop': 'equity',
                'title': 'equity',
            },
            {
                'prop': 'ev',
                'title': 'ev',
            },
            {
                'prop': 'suits_num',
                'title': '花色数',
            },
            {
                'prop': 'card_num_differ',
                'title': '相同牌',
            },
            {
                'prop': 'card_num_max',
                'title': '大牌',
            },
            {
                'prop': 'card_num_middle',
                'title': '中牌',
            },
            {
                'prop': 'card_num_min',
                'title': '小牌',
            },
            {
                'prop': 'card_total_num',
                'title': '点数和'
            },
            {
                'prop': 'card_connect_num',
                'title': '连接和',
            },
            {
                'prop': 'card_connect_max',
                'title': '最大连接',
            },
            {
                'prop': 'card_connect_min',
                'title': '最小连接',
            }
        ]
        for line in lines:
            if line[-1] == '\n':
                line = line[:-1]
            if line[-1] == '\t':
                line = line[:-1]
            flop_strategy_array = line.split('\t')
            flop_feature = get_flop_feature(flop_strategy_array[0])
            flop_strategy = {}
            flop_strategy['flop_board'] = flop_strategy_array[0]
            flop_strategy.update(flop_feature)
            flop_strategy['equity'] = flop_strategy_array[1]
            flop_strategy['ev'] = flop_strategy_array[2]
            array_len = len(flop_strategy_array)
            if array_len == 7:
                flop_strategy['bet_big'] = round(float(flop_strategy_array[3]), 2)
                flop_strategy['bet_middle'] = round(float(flop_strategy_array[4]), 2)
                flop_strategy['bet_small'] = round(float(flop_strategy_array[5]), 2)
                flop_strategy['check'] = round(float(flop_strategy_array[6]), 2)
            elif array_len == 8:
                flop_strategy['raise_big'] = round(float(flop_strategy_array[3]), 2)
                flop_strategy['raise_middle'] = round(float(flop_strategy_array[4]), 2)
                flop_strategy['raise_small'] = round(float(flop_strategy_array[5]), 2)
                flop_strategy['call'] = round(float(flop_strategy_array[6]), 2)
                flop_strategy['fold'] = round(float(flop_strategy_array[7]), 2)
            elif array_len == 9:
                flop_strategy['raise_all_in'] = round(float(flop_strategy_array[3]), 2)
                flop_strategy['raise_big'] = round(float(flop_strategy_array[4]), 2)
                flop_strategy['raise_middle'] = round(float(flop_strategy_array[5]), 2)
                flop_strategy['raise_small'] = round(float(flop_strategy_array[6]), 2)
                flop_strategy['call'] = round(float(flop_strategy_array[7]), 2)
                flop_strategy['fold'] = round(float(flop_strategy_array[8]), 2)
            flop_strategies.append(flop_strategy)
        if array_len == 7:
            table_column += [
                {
                    'prop': 'bet_big',
                    'title': '下大注',
                },
                {
                    'prop': 'bet_middle',
                    'title': '下中注',
                },
                {
                    'prop': 'bet_small',
                    'title': '下小注',
                },
                {
                    'prop': 'check',
                    'title': '过牌',
                }
            ]
        elif array_len == 8:
            table_column += [
                {
                    'prop': 'raise_big',
                    'title': '加大注',
                },
                {
                    'prop': 'raise_middle',
                    'title': '加中注',
                },
                {
                    'prop': 'raise_small',
                    'title': '加小注',
                },
                {
                    'prop': 'call',
                    'title': '跟注',
                },
                {
                    'prop': 'fold',
                    'title': '弃牌',
                }
            ]
        elif array_len == 9:
            table_column += [
                {
                    'prop': 'raise_all_in',
                    'title': '全压',
                },
                {
                    'prop': 'raise_big',
                    'title': '加大注',
                },
                {
                    'prop': 'raise_middle',
                    'title': '加中注',
                },
                {
                    'prop': 'raise_small',
                    'title': '加小注',
                },
                {
                    'prop': 'call',
                    'title': '跟注',
                },
                {
                    'prop': 'fold',
                    'title': '弃牌',
                }
            ]

        outputconfig = {
            'table_column': table_column,
            'flop_strategies': flop_strategies
        }

        filename1 = os.path.basename(file_full_path)[:-4]

        with open('strategies/' + folder_name+'/' + filename1 + '.json', 'w') as fJson:
            fJson.write(json.dumps(outputconfig))


def walk_sub_dir(folder_path, folder_name):
    if not os.path.exists('strategies/' + folder_name):
        os.makedirs('strategies/' + folder_name)
    for parent, dirs, files in os.walk(folder_path):
        for file in files:
            file_full_path = os.path.join(folder_path, file)
            analyze_poker_file(file_full_path, folder_name)


# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    for parent, dirs, file_names in os.walk('origin'):
        for dir in dirs:
            walk_sub_dir('origin/' + dir, dir)
    print_hi('1')

# See PyCharm help at https://www.jetbrains.com/help/pycharm/
